package it.CookBook;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

public class CookBookExplorer {

	public static void createNodes(File fileIn, ArrayList<String> top, ArrayList<NodeBean> listFile, boolean isRoot) {
		if (!fileIn.exists())
			return;
		FilenameFilter filenameFilter = new FilenameFilter() {

			public boolean accept(File dir, String name) {
				if (name.endsWith(".db") || name.endsWith(".bat") || name.endsWith(".jar"))
					return false;
				return true;
			}
		};
		File[] files = dirListByAscendingName(fileIn, filenameFilter);
		if (!isRoot)
			listFile.add(new NodeBean(fileIn, "UP"));
		
		if (files == null)
			return;
		
		for (int i = 0; i < files.length; i++) {
			if (files[i].isDirectory()) {
				//listFile.add(new NodeBean(files[i], files[i].getName()));
				File[] filestemp = dirListByAscendingName(files[i], filenameFilter);
				if (filestemp!= null && filestemp.length > 0 && allImagesAreFake(filestemp) == true)
				{
					for (int jk = 0; jk < filestemp.length; jk++) {
						String name = files[i].getName();
						if (filestemp.length > 1)
							name = name + " ("+(jk+1)+ ")";
						System.out.println("################"+filestemp[jk].getName()+" renaming in ->"+name);
						listFile.add(new NodeBean(filestemp[jk],name.toUpperCase()));
					}
				}
				else
				{
					System.out.println("DIR = " + files[i].getName());
					listFile.add(new NodeBean(files[i], files[i].getName().toUpperCase()));
					//createNodes(files[i], top, category, book);
				}
			} else if (files[i].isFile()) {
				String fileName = files[i].getName();
				System.out.println("Ricetta = " + fileName);
				if (fileName.toLowerCase().startsWith("image") && fileIn.isDirectory())
					fileName = fileIn.getName();
				listFile.add(new NodeBean(files[i], fileName.toUpperCase()));
			}
		}

	}

	public static File[] dirListByAscendingName(File folder,
			FilenameFilter filenameFilter) {
		if (!folder.isDirectory()) {
			return null;
		}
		File files[] = folder.listFiles(filenameFilter);
		if (files != null)
			Arrays.sort(files, new Comparator<Object>() {
				public int compare(final Object o1, final Object o2) {
					return new String(((File) o1).getName().toLowerCase()).compareTo(new String(((File) o2).getName().toLowerCase()));
				}
			});
		return files;
	}

	private static boolean allImagesAreFake(File[] files) {
		boolean returnValue = true;
		for (int i = 0; i < files.length; i++) {
			if (!files[i].getName().toLowerCase().startsWith("image") && !files[i].getName().toLowerCase().startsWith("001"))
				returnValue = false;
		}
		return returnValue;
	}

	public static void findFiles(File folder, String strFilter, ArrayList<File> filteredList) {
		File files[] = folder.listFiles();
		for (int i = 0; i < files.length; i++) {
			if (files[i].isDirectory()){
//				File[] filestemp = dirListByAscendingName(files[i], filenameFilter);
//				if (filestemp!= null && filestemp.length > 0 && allImagesAreFake(filestemp) == true)
//				{
//					for (int jk = 0; jk < filestemp.length; jk++) {
//						String name = files[i].getName();
//						if (filestemp.length > 1)
//							name = name + " ("+(jk+1)+ ")";
//						System.out.println("################"+filestemp[jk].getName()+" renaming in ->"+name);
//						listFile.add(new NodeBean(filestemp[jk],name));
//					}
//				}				
				findFiles(files[i], strFilter, filteredList);
			}
			else{
				if (files[i].getName().toLowerCase().contains(strFilter))
					filteredList.add(files[i]);
			}
		}
	}
}
